{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyN4zbA6UTpBoKhwBY7dto14",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sugarforever/LangChain-Tutorials/blob/main/LangChain_ChatOpenAI_OpenAI_Diff.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Difference between ChatOpenAI and OpenAI"
      ],
      "metadata": {
        "id": "KMYMb8dRTPds"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "In this notebook, I will explain with you the difference between the 2 classes introduced by LangChain framework."
      ],
      "metadata": {
        "id": "nMobxSKSTi8K"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "id": "0l3RTnY-vnsx"
      },
      "outputs": [],
      "source": [
        "!pip install langchain openai --quiet --upgrade"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain import OpenAI\n",
        "from langchain.chat_models import ChatOpenAI\n",
        "import os"
      ],
      "metadata": {
        "id": "R5j6nQZdvwTZ"
      },
      "execution_count": 20,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ['OPENAI_API_KEY'] = 'your openai api key'"
      ],
      "metadata": {
        "id": "gFoCDWd6xYHJ"
      },
      "execution_count": 21,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "ChatOpenAI is using OpenAI endpoint `/v1/chat/completions/` which supports the models:\n",
        "- gpt-4\n",
        "- gpt-4-0613\n",
        "- gpt-4-32k\n",
        "- gpt-4-32k-0613\n",
        "- gpt-3.5-turbo\n",
        "- gpt-3.5-turbo-0613\n",
        "- gpt-3.5-turbo-16k\n",
        "- gpt-3.5-turbo-16k-0613"
      ],
      "metadata": {
        "id": "Xki-BzX6Txcy"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "llm = ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-0613\")"
      ],
      "metadata": {
        "id": "2B4PVfNjRE1y"
      },
      "execution_count": 32,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain.schema import (\n",
        "    SystemMessage,\n",
        "    HumanMessage,\n",
        "    AIMessage\n",
        ")\n",
        "\n",
        "messages = [\n",
        "    HumanMessage(content=\"Hi AI, how are you today?\"),\n",
        "]"
      ],
      "metadata": {
        "id": "gfY1IydMRGnp"
      },
      "execution_count": 30,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "llm(messages)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_-zvodYHSQ2F",
        "outputId": "8f365b64-3811-4687-9230-4c8bd9630faa"
      },
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "AIMessage(content=\"Hello! As an AI, I don't have feelings, but I'm here and ready to assist you. How can I help you today?\", additional_kwargs={}, example=False)"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "OpenAI is using OpenAI endpoint `/v1/completions/` which supports the models:\n",
        " - text-davinci-003\n",
        " - text-davinci-002\n",
        " - text-curie-001\n",
        " - text-babbage-001\n",
        " - text-ada-001"
      ],
      "metadata": {
        "id": "PYbcPJdGUD16"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "llm = OpenAI(temperature=0, model_name='text-davinci-002')"
      ],
      "metadata": {
        "id": "0mnDmF8RRse0"
      },
      "execution_count": 34,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "llm('Hi AI, how are you today?')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "7YVluKNLRvyy",
        "outputId": "49c68d16-3da5-4820-a381-d28928e1ad6c"
      },
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"\\n\\nI'm doing well today. Thank you for asking.\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 35
        }
      ]
    }
  ]
}